package bravo.xc.com.xc_bravo_android.operation.re_password;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.ForgetBeans;
import bravo.xc.com.xc_bravo_android.beans.SendCodeBeans;
import bravo.xc.com.xc_bravo_android.operation.login.LoginActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RePasswordActivity extends BaseMVPActivity<RePasswordContract.RePasswordView,RePasswordContract.RePasswordPresenter> implements RePasswordContract.RePasswordView,View.OnClickListener{

    private EditText et_re_account;//登录名
    private EditText et_re_phoneNum;//手机号
    private EditText et_re_validationCode;//验证码
    private EditText et_re_newpassword;//新密码
    private TextView tv_re_sendValidationCode;//发送验证码
    private TextView tv_re_seePhone;
    private TextView tv_title_msg;//标题
    private Button btn_re_confirm;//提交
    private LinearLayout layout_title_left;
    private ProgressDialog progressDialog;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_re_seePhone:

                if(checkNull(R.id.tv_re_seePhone)){
                    showProgress(true);
                    SendCodeBeans sendCodeBeans = new SendCodeBeans();
                    sendCodeBeans.setUsername(et_re_account.getText().toString().trim());
                    presenter.doGetInit(sendCodeBeans);
                }
                break;
            case R.id.tv_re_sendValidationCode:

                if(checkNull(R.id.tv_re_seePhone)){
                    showProgress(true);
                    SendCodeBeans sendCodeBeans = new SendCodeBeans();
                    sendCodeBeans.setUsername(et_re_account.getText().toString().trim());
                    presenter.dosendCode(sendCodeBeans);
                }
                break;
            case R.id.btn_re_confirm:

                if(checkNull(R.id.btn_re_confirm)){
                    showProgress(true);
                    ForgetBeans forgetBeans = new ForgetBeans();
                    forgetBeans.setChackNumber(et_re_validationCode.getText().toString().trim());
                    forgetBeans.setNewPwd(et_re_newpassword.getText().toString().trim());
                    forgetBeans.setPhoneNum(et_re_phoneNum.getText().toString().trim());
                    forgetBeans.setUsername(et_re_account.getText().toString().trim());
                    presenter.doRepassword(forgetBeans);
                }
                break;
            case R.id.layout_title_left:
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected RePasswordContract.RePasswordPresenter createPresnter() {
        return new RePasswordPresenter();
    }

    @Override
    protected void initView() {
        progressDialog = new ProgressDialog(this,R.style.MyDialogStyle);

        et_re_account=(EditText)findViewById(R.id.et_re_account);
        et_re_phoneNum=(EditText)findViewById(R.id.et_re_phoneNum);
        tv_re_sendValidationCode=(TextView)findViewById(R.id.tv_re_sendValidationCode);
        et_re_validationCode=(EditText)findViewById(R.id.et_re_validationCode);
        et_re_newpassword=(EditText)findViewById(R.id.et_re_newpassword);
        tv_title_msg=(TextView)findViewById(R.id.tv_title_msg);
        tv_re_seePhone=(TextView)findViewById(R.id.tv_re_seePhone);
        btn_re_confirm=(Button)findViewById(R.id.btn_re_confirm);
        layout_title_left=(LinearLayout) findViewById(R.id.layout_title_left);
        tv_title_msg.setText("找回密码");
        tv_re_seePhone.setOnClickListener(this);
        tv_re_sendValidationCode.setOnClickListener(this);
        btn_re_confirm.setOnClickListener(this);
        layout_title_left.setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_re_password;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(RePasswordActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean checkNull(int id) {
        switch (id){
            case R.id.tv_re_seePhone:
                if(et_re_account.getText().toString().trim().isEmpty()){
                    showToast("请填写用户名");
                    return false;
                }
                break;
            case R.id.btn_re_confirm:
                if(et_re_account.getText().toString().trim().isEmpty()) {
                    showToast("请填写用户名");
                    return false;
                }
                if(et_re_phoneNum.getText().toString().trim().isEmpty()) {
                    Log.e("@@手机号为空","????");
                    showToast("未知错误：手机号为空");
                    return false;
                }
                if(et_re_validationCode.getText().toString().trim().isEmpty()) {
                    showToast("请填写您收到的验证码");
                    return false;
                }
                if(et_re_newpassword.getText().toString().trim().isEmpty()) {
                    showToast("请填写您的新密码");
                    return false;
                }
                break;
        }
        return  true;
    }

    @Override
    public void showProgress(boolean show) {

        if(progressDialog.isShowing()){
            progressDialog.dismiss();
            Log.e("@@showProgress","isShowing:dismiss");
        }
        if(show){
            progressDialog.show();
        }else {
            progressDialog.dismiss();
            Log.e("@@showProgress","dismiss");
        }

    }

    @Override
    public void setInfo(String username,String phone) {
        if(username!=null&&phone!=null){
            et_re_account.setText(username);
            et_re_phoneNum.setText(phone);
            et_re_phoneNum.setEnabled(false);
        }else {
            showToast("出现未知错误");
        }
    }

    @Override
    public void toNewActivity() {
        Intent toNewActivity = new Intent(RePasswordActivity.this,LoginActivity.class);
        startActivity(toNewActivity);
        this.finish();
    }
}
